package com.sunday.scoremanager.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.adapter.OrderAdapter;
import com.sunday.scoremanager.adapter.StaffAccountAdapter;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Order;
import com.sunday.scoremanager.entity.StaffAccount;
import com.sunday.scoremanager.http.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by 刘涛 on 2017/10/26.
 */

public class StaffManageActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private StaffAccountAdapter adapter;
    private LinearLayoutManager layoutManager;
    private List<StaffAccount> dataSet = new ArrayList<>();
    private long memberId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_list);
        ButterKnife.bind(this);
        initView();


    }

    private void initView() {
        titleView.setText("员工管理");
        rightTxt.setText("添加");
        rightTxt.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));

        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new StaffAccountAdapter(mContext, dataSet);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.shape_divider_width).build());
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getData();
            }
        });
        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });

        adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final StaffAccount item= (StaffAccount) v.getTag();
                switch (v.getId()){
                    case R.id.btn_edit:
                    intent=new Intent(mContext,AddAccountActivity.class);
                        intent.putExtra("account",item);
                        startActivity(intent);
                        break;
                    case R.id.btn_delete:
                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                        builder.setTitle("温馨提示");
                        builder.setMessage("确认删除此账号？");
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                deleteChildAccount(item.getId());
                            }
                        });
                        builder.show();
                        break;
                }
            }
        });

        memberId = BaseApp.getInstance().getMember().getSellerId();
        getData();

    }

    private void deleteChildAccount(long id){
        showLoadingDialog(0);
        Call<ResultDO> call=ApiClient.getApiAdapter().deleteAccount(id);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    getData();
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {

            }
        });

    }


    private void getData() {
        showLoadingDialog(0);
        Call<ResultDO<List<StaffAccount>>> call = ApiClient.getApiAdapter().accountList(memberId);
        call.enqueue(new Callback<ResultDO<List<StaffAccount>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<StaffAccount>>> call, Response<ResultDO<List<StaffAccount>>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                refreshLayout.finishRefresh();
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    dataSet.clear();
                    dataSet.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<StaffAccount>>> call, Throwable t) {
                refreshLayout.finishRefresh();
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @OnClick(R.id.rightTxt)
    void add() {
        intent = new Intent(mContext, AddAccountActivity.class);
        startActivity(intent);
    }

}
